from rest_framework import views
from rest_framework.response import Response
from rest_framework import authentication, permissions
from django.contrib.auth import get_user_model
from rest_framework.decorators import api_view

User = get_user_model()
class ListUsers(views.APIView):

    # authentication_classes = (authentication.TokenAuthentication,)
    # permission_classes = (permissions.IsAdminUser,)

    def get(self, request, format=None):

        url = "https://openapi.alipaydev.com/gateway.do?app_id=2016091800541924&biz_content=%7B%22subject%22%3A%22luffycity%22%2C%22out_trade_no%22%3A%2221101701zsdfaf32012311qwe41421349022223486%22%2C%22total_amount%22%3A100.0%2C%22product_code%22%3A%22FAST_INSTANT_TRADE_PAY%22%7D&charset=utf-8&method=alipay.trade.page.pay&notify_url=http%3A%2F%2F176.122.175.246%3A8000%2Fmytest%2Ftest6&return_url=http%3A%2F%2F127.0.0.1%3A8000%2Fmytest%2Ftest6%2F&sign_type=RSA2&timestamp=2018-08-09+07%3A05%3A51&version=1.0&sign=DOJ5XTSm6uAo4%2FJYJKbeyWHrXJhYwM1h2T%2FJ7Gck92yprliP7m3jshTxj8FPfLv4gkERDkvOuRh2RsE15AYEIO18Q2IHwn75DFgQUFZ7TlomBVab%2BOqb0iQ60MWlOZYIvJ4JENm36udTYsZw5Z88m32Wo%2F3bxFqwCDQ%2FF1MHVaoiafwgIiYgXp6ffc%2FZqiSCRhbwWwiz2RcADCVprZpjKoCrJBqn6523qhQiXZ6K6eQJrq8N66Oh3yn7L5Z5UzNCShcaZFOaO98eUiNDBmRvgHbBcoGhlrHQ0fepjrFLX6PaWI%2BDdNdtW9Z8eONGTw004p3l%2BEKOhUPjL4EPvnbstA%3D%3D"
        return Response(url)

    def post(self, request, format=None):
        return Response({"post": "Hello, world!"})

    def ggg(self):
        return Response({"post": "Hello, world!"})





@api_view()
def hello_world(request):
    return Response({"message": "Hello, world!"})


@api_view(['GET', 'POST'])
def hello_worldV2(request):
    if request.method == 'POST':
        return Response({"message": "Hello, world! POST"})

    if request.method == "GET":
        return Response({"message": "Hello, world! GET"})
    return Response({"message": "Hello, world!"})


from rest_framework.throttling import UserRateThrottle


class OncePerDayUserThrottle(UserRateThrottle):
    rate = '30/minute'


class ListUsersV2(views.APIView):
    # 通过什么方式来验证用户,这个例子里面是通过token来验证用户信息
    authentication_classes = (authentication.SessionAuthentication,)
    # 用户必须是什么级别才能使用这个接口
    permission_classes = (permissions.IsAdminUser, permissions.IsAuthenticatedOrReadOnly)
    throttle_classes = (OncePerDayUserThrottle,)

    # def get(self, request, format=None):
    #     return Response({"get": "Hello, world!"})

    # if request.method == 'POST':
    def post(self, request):
        return Response({"message": "Hello, world! POST"})

    def get(self,request):
        return Response({"message": "Hello, world! GET"})






